package com.lingxu.base.config;

import com.lingxu.base.common.util.IPUtils;
import com.lingxu.base.common.util.SpringContextUtils;
import com.lingxu.base.system.common.entity.SysLog;
import com.lingxu.base.system.common.service.ISysLogService;
import com.lingxu.base.system.manage.vo.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Title: LogInfoHandler
 * @Package: com.lingxu.common.config
 * @Author: ysj闫世杰
 * @Email: 1105324813@qq.com
 * @Date: Created in 2022/4/19 15:50
 * @Description:
 * @Version: 1.0.0
 */
@Component
public class LogInfoHandler {

    @Resource
    private ISysLogService iSysLogService;
    private static LogInfoHandler logInfoHandler;

    //通过@PostConstruct实现初始化bean之前进行的操作
    @PostConstruct
    public void init() {
        logInfoHandler = this;
    }

    public void setLogInfoHandler(int logType,String content){
        Date date = new Date();
        LoginUser userNow = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        SysLog sysLog = new SysLog();
        HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
        sysLog.setIp(IPUtils.getIpAddr(request));
        sysLog.setUserid(userNow.getId());
        sysLog.setUsername(userNow.getUsername());
        sysLog.setLogtype(logType);
        sysLog.setCreatetime(date);
        sysLog.setCreateby(userNow.getId());
        sysLog.setRealname(userNow.getName());
        String text = "用户：" + userNow.getName();
        sysLog.setLogcontent(text + content);
        logInfoHandler.iSysLogService.save(sysLog);
    }
    public void setLogInfoHandler(int logType,int operatetype,String content){
        Date date = new Date();
        LoginUser userNow = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        SysLog sysLog = new SysLog();
        HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
        sysLog.setIp(IPUtils.getIpAddr(request));
        sysLog.setUserid(userNow.getId());
        sysLog.setUsername(userNow.getUsername());
        sysLog.setLogtype(logType);
        sysLog.setOperatetype(operatetype);
        sysLog.setCreatetime(date);
        sysLog.setCreateby(userNow.getId());
        sysLog.setRealname(userNow.getName());
        String text = "用户：" + userNow.getName();
        sysLog.setLogcontent(text + content);
        logInfoHandler.iSysLogService.save(sysLog);
    }
//    public void setLogInfoHandler(int logType,int operatetype,String content,long caseid){
//        Date date = new Date();
//        LoginUser userNow = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//        SysLog sysLog = new SysLog();
//        HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
//        sysLog.setIp(IPUtils.getIpAddr(request));
//        sysLog.setUserid(userNow.getId());
//        sysLog.setUsername(userNow.getUsername());
//        sysLog.setLogtype(logType);
//        sysLog.setOperatetype(operatetype);
//        sysLog.setCreatetime(date);
//        sysLog.setCreateby(userNow.getId());
//        sysLog.setRealname(userNow.getName());
//        String text = "用户：" + userNow.getName();
//        sysLog.setLogcontent(text + content);
//        logInfoHandler.iSysLogService.save(sysLog);
//    }
}
